package com.army.stock.web;

import com.alibaba.fastjson.JSON;
import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmyDepartment;
import com.army.stock.domain.ArmyInstorageOrder;
import com.army.stock.domain.ArmySupplier;
import com.army.stock.service.*;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.util.date.DateFormatUtils;
import com.javalife.common.web.url.BaseUrl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by zhangyong on 15/3/7.
 * 军队库存入库管理相关
 */
@Controller
public class ArmyInStorageController {

    private static final Logger logger = LoggerFactory.getLogger(ArmyInStorageController.class);

    private static final String INSTORAGE_LIST = "instorage/list";

    private static final String INSTORAGE_NEW = "instorage/new";

    private static final String INSTORAGE_AVAILABLES = "instorage/availables";

    private static final String CHOOSE_GOOD = "instorage/choosegood";

    private static final String INSTORAGE_DETAIL = "instorage/detail";

    @Autowired
    private ArmyInstorageOrderService armyInstorageOrderService;

    @Autowired
    private ArmyMonthplanService armyMonthplanService;

    @Autowired
    private ArmyDepartmentService armyDepartmentService;

    @Autowired
    private ArmySupplierService armySupplierService;

    @Autowired
    private ArmyCategoryService armyCategoryService;

    @RequestMapping(value = "/instorage/list", method = RequestMethod.GET)
    public String instorageList(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyCategory> pageInfo = armyInstorageOrderService.getInstorageOrderPageInfo(
                baseUrl.getCurrentPage(), 20, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return INSTORAGE_LIST;
    }

    @RequestMapping(value = "/instorage/choosegood")
    public String choosegood(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        Calendar calendar = Calendar.getInstance();
        String month = request.getParameter("month");
        Map<String, Object> queryMap = baseUrl.getQueryMap();
        if (StringUtils.isEmpty(month)) {
            //若无时间则选择当前月
            queryMap.put("month", calendar.get(Calendar.MONTH) + 1);
        }
        queryMap.put("year", calendar.get(Calendar.YEAR));
        modelMap.put("baseUrl", baseUrl);
        logger.info("添加入库单签查看可入库的商品:{}" ,queryMap);
        PaginatedArrayList pageInfo = armyMonthplanService.getArmyMonthplanItemsPageInfoForInstorage(baseUrl.getCurrentPage(), 20, queryMap);
        modelMap.put("page", pageInfo);
        modelMap.put("month", queryMap.get("month"));
        modelMap.put("searchGoodName", queryMap.get("searchGoodName"));
        modelMap.put("baseUrl", baseUrl);
        return CHOOSE_GOOD;
    }

    @RequestMapping(value = "/instorage/new", method = RequestMethod.GET)
    public String addInstoragePage(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        List<ArmyDepartment> armyDepartments = armyDepartmentService.getAllArmyDepartments();
        modelMap.put("armyDepartments", armyDepartments);

        List<ArmySupplier> suppliers = armySupplierService.getArmySuppliers(null);

        ArmyInstorageOrder armyInstorageOrder = new ArmyInstorageOrder();
        armyInstorageOrder.setInstorageDate(DateFormatUtils.format(new Date(), "yyyyMMdd"));
        armyInstorageOrder.setCode(armyInstorageOrderService.getInstorageOrderCode());
        modelMap.put("armyInstorageOrder", armyInstorageOrder);
        modelMap.put("baseUrl", baseUrl);
        modelMap.put("suppliers", suppliers);

        return INSTORAGE_NEW;
    }

    @RequestMapping(value = "/instorage/add", method = RequestMethod.POST)
    @ResponseBody
    public String addInstorage(HttpServletRequest request, ModelMap modelMap) {
        String instorageJson = request.getParameter("instorageJson");
        logger.info("addInstorageJson:{}", instorageJson);
        Map<String, Object> returnMap = new HashMap<>();
        try {
            armyInstorageOrderService.addInstrageOrder(instorageJson);
            returnMap.put("code", "0");
        } catch (Exception e) {
            logger.error("addInstorage error:" + e.getMessage());
            returnMap.put("code", "-1");
        }
        return JSON.toJSONString(returnMap);
    }

    @RequestMapping(value = "/instorage/detail", method = RequestMethod.POST)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        return INSTORAGE_DETAIL;
    }

    @RequestMapping(value = "/instorage/availables", method = RequestMethod.GET)
    public String availables(HttpServletRequest request, ModelMap modelMap) {
        String departmentId = request.getParameter("departmentId");
        return INSTORAGE_AVAILABLES;
    }

}
